﻿namespace Microsoft.VisualStudio.ServiceModel.DomainServices.Tools
{
    using EnvDTE;
    using EnvDTE80;
    using Microsoft.VisualStudio.ManagedInterfaces9;
    using Microsoft.VisualStudio.OLE.Interop;
    using Microsoft.VisualStudio.Shell;
    using Microsoft.VisualStudio.Shell.Interop;
    using Microsoft.VisualStudio.TemplateWizard;
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.IO;
    using System.Threading;
    using System.Windows.Forms;
    using System.Windows.Forms.Design;

    internal class AuthenticationDomainServiceWizard : IWizard
    {
        private DTE2 _dte2;
        private Project _project;

        public void BeforeOpeningFile(ProjectItem projectItem)
        {
        }

        private object GetService(System.Type serviceType)
        {
            Microsoft.VisualStudio.OLE.Interop.IServiceProvider sp = this.Dte2 as Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
            if (sp == null)
            {
                return null;
            }
            using (ServiceProvider provider2 = new ServiceProvider(sp))
            {
                return provider2.GetService(serviceType);
            }
        }

        public void ProjectFinishedGenerating(Project project)
        {
        }

        public void ProjectItemFinishedGenerating(ProjectItem projectItem)
        {
            this._project = projectItem.ContainingProject;
        }

        public void RunFinished()
        {
            Microsoft.VisualStudio.OLE.Interop.IServiceProvider sp = this.Dte2 as Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
            IVsApplicationConfigurationManager manager = null;
            IVsHierarchy ppHierarchy = null;
            using (ServiceProvider provider2 = new ServiceProvider(sp))
            {
                IVsSolution service = provider2.GetService(typeof(IVsSolution)) as IVsSolution;
                if (service != null)
                {
                    manager = provider2.GetService(typeof(IVsApplicationConfigurationManager)) as IVsApplicationConfigurationManager;
                    if ((manager != null) && ((service.GetProjectOfUniqueName(this._project.UniqueName, out ppHierarchy) == 0) && (ppHierarchy != null)))
                    {
                        goto Label_0083;
                    }
                }
                return;
            }
        Label_0083:
            using (IVsApplicationConfiguration configuration = manager.GetApplicationConfiguration(ppHierarchy, 0xfffffffe))
            {
                if ((configuration != null) && configuration.FileExists())
                {
                    System.Configuration.Configuration configuration2 = configuration.LoadConfiguration();
                    if (configuration2 != null)
                    {
                        WebConfigUtil util = new WebConfigUtil(configuration2);
                        bool flag = util.DoWeNeedToAddHttpModule();
                        bool flag2 = util.DoWeNeedToAddModuleToWebServer();
                        bool flag3 = !util.IsAspNetCompatibilityEnabled();
                        bool flag4 = !util.IsMultipleSiteBindingsEnabled();
                        bool flag5 = util.DoWeNeedToValidateIntegratedModeToWebServer();
                        if ((flag || flag2) || ((flag3 || flag4) || flag5))
                        {
                            string domainServiceModuleTypeName = WebConfigUtil.GetDomainServiceModuleTypeName();
                            configuration.QueryEditConfiguration();
                            if (flag)
                            {
                                util.AddHttpModule(domainServiceModuleTypeName);
                            }
                            if (flag2)
                            {
                                util.AddModuleToWebServer(domainServiceModuleTypeName);
                            }
                            if (flag3)
                            {
                                util.SetAspNetCompatibilityEnabled(true);
                            }
                            if (flag4)
                            {
                                util.SetMultipleSiteBindingsEnabled(true);
                            }
                            if (flag5)
                            {
                                util.AddValidateIntegratedModeToWebServer();
                            }
                            configuration2.Save();
                        }
                    }
                }
            }
        }

        public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            this._dte2 = (DTE2) automationObject;
            string str = replacementsDictionary["$rootname$"];
            str = str.Trim();
            if (string.IsNullOrEmpty(str))
            {
                this.TerminateWizard(Resources.WizardError_Empty_Filename);
            }
            if (string.IsNullOrEmpty(Path.GetFileNameWithoutExtension(str)))
            {
                this.TerminateWizard(Resources.WizardError_Empty_Filename);
            }
        }

        public bool ShouldAddProjectItem(string filePath)
        {
            return true;
        }

        private void ShowError(string errorMessage)
        {
            IUIService service = (IUIService) this.GetService(typeof(IUIService));
            if (service != null)
            {
                MessageBoxOptions options = 0;
                IWin32Window dialogOwnerWindow = service.GetDialogOwnerWindow();
                if (System.Threading.Thread.CurrentThread.CurrentUICulture.TextInfo.IsRightToLeft)
                {
                    options |= MessageBoxOptions.RightAlign;
                    options |= MessageBoxOptions.RtlReading;
                }
                MessageBox.Show(dialogOwnerWindow, errorMessage, Resources.WizardError_Caption, MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1, options);
            }
        }

        private void TerminateWizard(string errorMessage)
        {
            if (!string.IsNullOrEmpty(errorMessage))
            {
                this.ShowError(errorMessage);
                throw new WizardCancelledException(errorMessage);
            }
            throw new WizardCancelledException();
        }

        private DTE2 Dte2
        {
            get
            {
                if (this._dte2 == null)
                {
                    this.TerminateWizard(Resources.WizardError_No_DTE);
                }
                return this._dte2;
            }
        }
    }
}

